<?php

class kyoMail extends \PHPMailer
{
    static private $instrance = null;

    public function __construct()
    {
        parent::__construct();
        $this->isSMTP();
        $this->Host = 'smtp.163.com';
        $this->Port = 25;
        // $this->SMTPSecure = 'tls';
        $this->SMTPAuth = true;
        $this->CharSet = 'UTF-8';
        $this->Encoding = 'base64';

        $this->Username = 'm211E068009B@163.com';
        $this->Password = '211E068009B';

        $this->setFrom('m211E068009B@163.com', 'System');

        $this->isHTML = true;
    }

    static public function getInstrance()
    {
        if (self::$instrance === null)
            self::$instrance = new kyoMail();

        return self::$instrance;
    }

    public function sendTo($recvmail, $title, $body = null)
    {
        $files = [];
        if ($body === null)
            $body = $title;
        else if (is_array($body)) {
            $files = $body['files'] ?? [];
            $body = $body['content'] ?? $title;
        }

        $this->addAddress($recvmail);
        $this->Subject = $title;
        $this->msgHTML($body);

        foreach ($files as $f) {
            $this->addAttachment($f);
        }

        if (!$this->send())
            return $this->ErrorInfo;
        return true;
    }
}
